import java.util.Arrays;

public class Solution739 {
    public int[] dailyTemperatures(int[] temperatures) {
        int[] ans=new int[temperatures.length];
        int[] stack=new int[temperatures.length];
        int top=0;
        for (int i=temperatures.length-1;i>=0;i--){
            while (top!=0 && temperatures[i]>=temperatures[stack[top-1]]){
                top--;
            }
            if (top==0){
                ans[i]=0;
            }else{
                ans[i]=stack[top-1]-i;
            }
            stack[top]=i;
            top++;
        }
        return ans;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution739().dailyTemperatures(new int[]{73, 74, 75, 71, 69, 72, 76, 73})));
    }
}
